local session = require "sailor.session"
local validation = require "valua"
local form = require "sailor.form"

local user = {}

function user.index(page)
    page:render('index')
end

function user.list(page)
    -- 如果是post，将数据存储到数据库中并以表单形式显示本数据
    -- 如果是get，直接渲染数据表单
    local User = sailor.model("user")
	if next(page.POST) then
        local u = User:new()
        u.username = page.POST['user:username']
        u.password = page.POST['user:password']
        u:save()
        page:render('new',{user=u,form = form})
    else
        --us = User:find_all(username='xing')
        --page:render('list',{user=us})
        page:write('users')
	end
end

function user.new(page)
	local User = sailor.model("user")
	local u = User:new()
	u.username = "user"
	u.password = "pass"
    page:render('new',{user=u,form = form})
end

function user.models(page)
	--usering Models
    --[[
		I'm using 'User' model for usering under a mysql db.	
		If you want to check it out, you must import the sql files under /sql		
    ]]
    local User = sailor.model("user")
    local u = User:new()

    u.username = "maria"
    u.password = "12345678"

    local res,errs = u:validate()
    if not res then
    	page:write("failed user!<br/>")
    else
    	page:write("passed user!<br/>")
    end

    if u:save() then
        page:write("saved! "..u.id.."<br/>")
    end
    
    -- FIND() IS NOT YET ESCAPED, DONT USE IT UNLESS YOU WROTE THE 'WHERE' STRING YOURSELF
    local u2 = User:find("username ='francisco'")

    if u2 then
        page:write(u2.id.." - "..u2.username.."<br/>")
    end

    local users = User:find_all()
    for _, user in pairs(users) do 
        page:write(user.id.." - "..user.username.."<br/>")
    end
      
    u.username = "catarina"
    if u:save() then
        page:write("saved! "..u.id.."<br/>")
    end

    local users = User:find_all()
    for _, user in pairs(users) do 
        page:write(user.id.." - "..user.username.."<br/>")
    end

    page:write("Finding user with id 1:<br/>")
    local some_user = User:find_by_id(1)
    if some_user then
        page:write(some_user.id.." - "..some_user.username.."<br/>")
    end

    page:write("Finding user with id 47:<br/>")
    local some_user = User:find_by_id(47)
    if some_user then
        page:write(some_user.id.." - "..some_user.username.."<br/>")
    else
        page:write("User not found!")
    end
end

function user.modelval(page)
	local User = sailor.model("user")
    local u = User:new()
    u.username = ""
    u.password = "12345"
    local res,err = u:validate()
    page:print(table.concat(err,'<br/>'), '<br/>')
    u.username = "Lala"
    u.password = "12345"
    local res,err = u:validate()
    page:print("<br/>",unpack(err))
    u.username = "Lala"
    u.password = "12345678"
    local res,err = u:validate()
    page:print("<br/>",unpack(err or {}))
end


function user.redirect(page)
	return page:redirect('user',{hey="blah",hue = 2})
end

function user.include(page)
    page:render('include')
end

function user.error(page)
    page:render('error')
end

function user.newsession(page) 
    session.open(page.r)
    session.save({username = "john lennon"})             
    if session.data then
        for k,v in pairs(session.data) do
            page:write(v)
        end
    end
end

function user.opensession(page) 
    session.open(page.r)
    if session.data then
        for k,v in pairs(session.data) do
            page:write(v)
        end
    end
end

function user.destroysession(page) 
    session.destroy(page.r)
end

function user.login(page)
    local access = require "sailor.access"
    if access.is_guest() then
        page:print("Logging in...<br/>")
        local _,err = access.login("demo","demo")
        page:print(err or "Logged in.")
    else
        page:print("You are already logged in.")
    end
end

function user.runat_client(page)
    page:render('runat_client')
end

function user.runat_both(page)
    page:render('runat_both')
end

function user.client_module(page)
    page:render('client_module')
end

function user.client_module_js(page)
    page:render('client_module_js')
end


return user
